(function() {
    'use strict';

    /**
     * @ngdoc overview
     * @name webApp
     * @description
     * # webApp
     *
     * Main module of the application.
     */
    angular
        .module('webApp', [
            'ngCookies',
            'ngMessages',
            'ngResource',
            'ngAnimate',
            'ui.router',
            'http-auth-interceptor',
            'pascalprecht.translate',
            'angular-cache',
            'angular-md5',
            'angularMoment',
            'ab-base64',
            'ngCors'
        ])
        .config(function($stateProvider, $urlRouterProvider) {
            // For any unmatched url, send to /route1
            $urlRouterProvider.otherwise("/shop/list");
            _.map(_erp_router, function(cf, name) {
                $stateProvider.state(name, cf);
            });
        })
        .run(['$state', '$cookies', '$rootScope',
            function($state, $cookies, $rootScope) {
                $rootScope.$on('$stateChangeSuccess', function(e, toState, toParams, fromState, fromParams) {
                    $rootScope.$broadcast("event:state_changed", toState);
                });
            }
        ])
        .config(['$translateProvider',
            function($translateProvider) {
                var lang = window.localStorage.lang || 'zh-cn';
                $translateProvider.preferredLanguage(lang);
                $translateProvider.useStaticFilesLoader({
                    prefix: '../i18n/',
                    suffix: '.json'
                });
                $translateProvider.useSanitizeValueStrategy('escape');
            }
        ])
        .config(function($compileProvider) {
            $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|javascript):/);
        })
        .constant('AUTH_EVENTS', {
            loginSuccess: 'auth-login-success',
            loginFailed: 'auth-login-failed',
            logoutSuccess: 'auth-logout-success',
            sessionTimeout: 'auth-session-timeout',
            notAuthenticated: 'auth-not-authenticated',
            notAuthorized: 'auth-not-authorized'
        })
        .constant('USER_ROLES', {
            all: '*',
            admin: 'admin',
            user: 'user',
            guest: 'guest'
        })
        .config(function($httpProvider) {
            function serialize(obj, prefix) {
                var str = [];
                for (var p in obj) {
                    if (obj.hasOwnProperty(p)) {
                        var k = prefix ? prefix + "[" + p + "]" : p,
                            v = obj[p];
                        str.push(typeof v == "object" ?
                            serialize(v, k) :
                            encodeURIComponent(k) + "=" + encodeURIComponent(v));
                    }
                }
                return str.join("&");
            }

            // send all requests payload as query string
            $httpProvider.defaults.transformRequest = function(data) {
                if (data === undefined) {
                    return data;
                }
                return serialize(data);
            };

            // set all post requests content type
            $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

        }).constant('angularMomentConfig', {
            timezone: moment.tz.guess() // e.g. 'Europe/London'
        })
        .run(function(amMoment) {
            var lang = window.localStorage.lang || 'zh-cn';
            amMoment.changeLocale(lang);
        });

    //console.log("app.js 执行完毕");
})();
